/*
 * Copyright (C) 2012-2013 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package py.client.thrift;

import com.google.common.base.Charsets;
import java.net.InetAddress;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;

public class SocksProtocols {
  public static final int SOCKS_VERSION_4 = 0x04;
  public static final int CONNECT = 0x01;
  public static final int REQUEST_GRANTED = 0x5a;
  public static final int REQUEST_FAILED = 0x5b;
  public static final int REQUEST_FAILED_NO_IDENTD = 0x5c;
  public static final int REQUEST_FAILED_USERID_NOT_CONFIRMED = 0x5d;

  public static ChannelBuffer createSocks4packet(InetAddress address, int port) {
    if (address == null) {
      throw new IllegalArgumentException("address is null");
    }
    byte[] userBytes = System.getProperty("user.name", "").getBytes(Charsets.ISO_8859_1);
    ChannelBuffer handshake = ChannelBuffers.dynamicBuffer(9 + userBytes.length);
    handshake.writeByte(SOCKS_VERSION_4);
    handshake.writeByte(CONNECT);
    handshake.writeShort(port);
    handshake.writeBytes(address.getAddress());
    handshake.writeBytes(userBytes);
    handshake.writeByte(0x00);
    return handshake;
  }

  public static ChannelBuffer createSock4aPacket(String hostName, int port) {
    if (hostName == null) {
      throw new IllegalArgumentException("hostName is null");
    }
    byte[] userBytes = System.getProperty("user.name", "").getBytes(Charsets.ISO_8859_1);
    byte[] hostNameBytes = hostName.getBytes(Charsets.ISO_8859_1);
    ChannelBuffer handshake = ChannelBuffers
        .dynamicBuffer(10 + userBytes.length + hostNameBytes.length);
    handshake.writeByte(SOCKS_VERSION_4);
    handshake.writeByte(CONNECT);
    handshake.writeShort(port);
    handshake.writeByte(0x00);
    handshake.writeByte(0x00);
    handshake.writeByte(0x00);
    handshake.writeByte(0x01);
    handshake.writeBytes(userBytes);
    handshake.writeByte(0x00);
    handshake.writeBytes(hostNameBytes);
    handshake.writeByte(0x00);
    return handshake;
  }
}
